package com.cauli.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cauli.file.component.FileComponent;
import com.cauli.file.component.FileDealComp;
import com.cauli.file.mapper.RecoveryFileMapper;
import com.cauli.file.mapper.UserFileMapper;
import com.cauli.file.model.entity.RecoveryFile;
import com.cauli.file.model.entity.UserFile;
import com.cauli.file.model.vo.recovery.RecoveryFileListVO;
import com.cauli.file.service.RecoveryFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Cauli
 * @date 2023-03-20 12:00
 * @description 回收站 服务实现类
 */
@Slf4j
@Service
public class RecoveryFileServiceImpl extends ServiceImpl<RecoveryFileMapper, RecoveryFile> implements RecoveryFileService {
    @Autowired
    private UserFileMapper userFileMapper;

    @Resource
    private FileDealComp fileDealComp;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserFileByDeleteBatchNum(String deleteBatchNum) {
        LambdaQueryWrapper<UserFile> userFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userFileLambdaQueryWrapper.eq(UserFile::getDeleteBatchNum, deleteBatchNum);
        List<UserFile> userFileList = userFileMapper.selectList(userFileLambdaQueryWrapper);

        for (UserFile userFile : userFileList) {
            userFileMapper.deleteById(userFile);
            // ------------------------删除ES数据------------------------
            fileDealComp.deleteES(userFile.getUserFileId());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void restoreFile(String deleteBatchNum, String filePath, Long userId) {
        LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userFileLambdaUpdateWrapper
                .set(UserFile::getDeleteFlag, 0)
                .set(UserFile::getDeleteBatchNum, "")
                .eq(UserFile::getDeleteBatchNum, deleteBatchNum);
        userFileMapper.update(null, userFileLambdaUpdateWrapper);

        FileComponent fileComponent = new FileComponent(filePath, true);
        fileDealComp.restoreParentFilePath(fileComponent, userId);

        fileDealComp.deleteRepeatSubDirFile(filePath, userId);
        // TODO 如果被还原的文件已存在，暂未实现

        LambdaQueryWrapper<RecoveryFile> recoveryFileServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        recoveryFileServiceLambdaQueryWrapper.eq(RecoveryFile::getDeleteBatchNum, deleteBatchNum);
        baseMapper.delete(recoveryFileServiceLambdaQueryWrapper);
    }

    @Override
    public List<RecoveryFileListVO> selectRecoveryFileList(Long userId) {
        return baseMapper.selectRecoveryFileList(userId);
    }
}
